package apis

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"tylnmall/models"
	"common/logger"
	"net/http"
	"tylnmall/constant"
	"errors"
	"math"
)

//文章详情
func MesApi(c *gin.Context) {
	type param struct {
		Id int `json:"id" form:"id"`
	}
	var p param
	var err error
	var mes models.Message
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	if err = db.Where("id = ?", p.Id).Find(&mes).Error; err != nil {
		logger.Error(err)
		err = errors.New("message not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"mes": mes})

}

//文章名称列表
func MesNameListByTypeApi(c *gin.Context) {
	type param struct {
		Id   int `json:"id" form:"id"`
		Page int `json:"page" form:"page"`
	}
	type NameList struct {
		Id   int    `json:"id"`
		Name string `json:"name"`
	}
	var err error
	var p param
	var mes []*models.Message
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	db := c.MustGet("ty").(*gorm.DB)
	var count float64
	if err := db.Where("type_id = ?", p.Id).Model(&models.Message{}).Count(&count).Error; err != nil {
		logger.Error(err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	if p.Page == 0 {
		p.Page = 1
	} else if float64(p.Page) > math.Ceil(count/10.0) {
		p.Page = 1
	}
	if err = db.Limit(10).Offset((p.Page - 1) * 10).Where("type_id = ?", p.Id).Find(&mes).Error; err != nil {
		logger.Error(err)
		err = errors.New("message not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	//var list []*NameList
	for _,m := range mes{
		m.Mes = ""
	}
	c.JSON(http.StatusOK, gin.H{"list": mes, "pageNum": math.Ceil(count / 10.0)})
}

//文章类别列表
func MesTypeListApi(c *gin.Context) {
	var mes []*models.MessageType
	db := c.MustGet("ty").(*gorm.DB)
	if err := db.Find(&mes).Error; err != nil {
		logger.Error(err)
		err = errors.New("messageType not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"list": mes})
}

//根据文章种类获取文章分类
func MesTypeListByTypeApi(c *gin.Context){
	type param struct {
		Type int `json:"type" form:"type"`
	}
	var p param
	var err error
	if err = c.Bind(&p); err != nil {
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorParamWrong, "err_msg": constant.TranslateErrCode(constant.ErrorParamWrong)})
		return
	}
	var mes []*models.MessageType
	db := c.MustGet("ty").(*gorm.DB)
	if err := db.Where("type = ?",p.Type).Find(&mes).Error; err != nil {
		logger.Error(err)
		err = errors.New("messageType not found")
		logger.Error(err)
		c.JSON(http.StatusOK, gin.H{"err_code": constant.ErrorDB, "err_msg": constant.TranslateErrCode(constant.ErrorDB)})
		return
	}
	c.JSON(http.StatusOK, gin.H{"list": mes})
}
